package com.junchenrobot.mes.dto;

import com.junchenrobot.mes.config.AsyncPool;
import com.junchenrobot.mes.core.TaskExecutorCenter;
import com.junchenrobot.mes.core.TaskScheduled;
import com.junchenrobot.mes.dao.AgvWorkDao;
import com.junchenrobot.mes.dao.DeviceDao;
import com.junchenrobot.mes.dao.WinderTaskDao;
import com.junchenrobot.mes.dao.WinderTaskRunDao;
import com.junchenrobot.mes.domain.*;
import com.junchenrobot.mes.enums.PoolStateEnum;
import com.junchenrobot.mes.listener.source.WinderTaskRedisRepository;
import com.junchenrobot.mes.util.SpringContentUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

@Slf4j
public class PoolTask implements Runnable {

    private final AgvWorkDao agvWorkDao = SpringContentUtil.getBean("agvWorkDao", AgvWorkDao.class);
    private final DeviceDao deviceDao = SpringContentUtil.getBean("deviceDao", DeviceDao.class);
    private final WinderTaskDao winderTaskDao = SpringContentUtil.getBean("winderTaskDao", WinderTaskDao.class);
    private final WinderTaskRunDao winderTaskRunDao = SpringContentUtil.getBean("winderTaskRunDao", WinderTaskRunDao.class);
    private final TaskScheduled taskScheduled = SpringContentUtil.getBean("taskScheduled", TaskScheduled.class);
    private final TaskExecutorCenter executorCenter = SpringContentUtil.getBean("taskExecutorCenter",
            TaskExecutorCenter.class);

    private final String taskCode;

    private final Long winderId;

    public PoolTask(String taskCode, Long winderId) {
        this.taskCode = taskCode;
        this.winderId = winderId;
    }

    @Autowired
    WinderTaskRedisRepository winderTaskRedisRepository;
//    @Autowired
//    WinderTaskRunDao winderTaskRunDao;

    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        AgvWork agvWork = agvWorkDao.getAgvWork(winderId);
        if (Objects.isNull(agvWork) || agvWork.getStatus() == 4) {
            log.info("人工拿走!");
            WinderTask winderTask = winderTaskDao.getTask(taskCode);
            WinderTaskRun winderTaskRun = WinderTaskRun.builder().build();
            BeanUtils.copyProperties(winderTask, winderTaskRun);
//            WinderTaskRun task = winderTaskRunDao.getWinderTask(winderTask.getId());
//            log.info("task:{}",task);
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                System.exit(0); //退出程序
            }
            //kc删除任务
            executorCenter.cancelTask(taskCode);
            //删除存放小车任务的redis中人工拿走的任务
            winderTaskDao.deleteWinderTask(taskCode);
            //删除存放在小车中的任务
            winderTaskRunDao.deleteWinderTask(winderTaskRun);
            Integer taskPoleCode = winderTask.getTaskPoleCode();
            Integer taskType = winderTask.getTaskType();
            if (taskType==1){//取料
                deviceDao.updateDevicePoleStatus(winderTask.getTaskDeviceName(), taskPoleCode, PoolStateEnum.FEED_FAILURE.getState());
            }else {
                deviceDao.updateDevicePoleStatus(winderTask.getTaskDeviceName(), taskPoleCode, PoolStateEnum.COMPLETION_OF_TAKE_UP.getState());
            }
            boolean cancel = AsyncPool.cancel(taskCode);
            log.info("取消定时任务{}", cancel);
        } else {
            log.info("检查{}", winderId);
        }
    }

}
